<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace hdphp\utils;

/**
 * Class：数组帮助类
 * 创建人：Jack
 * 创建时间：2023/7/29 7:05 PM
 * @package hdphp\utils
 */
class Arr
{

    /**
     * 注释：对数组增加默认值
     * 创建人：Jack
     * 创建时间：2023/7/29 7:06 PM
     * @param array $keys
     * @param array $configList
     * @return array
     */
    public static function getDefaultValue(array $keys, array $configList = []): array
    {
        $value = [];
        foreach ($keys as $val) {
            if (is_array($val)) {
                $k = $val[0] ?? '';
                $v = $val[1] ?? '';
            } else {
                $k = $val;
                $v = '';
            }
            $value[$k] = $configList[$k] ?? $v;
        }
        return $value;
    }

    /**
     * 注释：获取ivew菜单列表
     * 创建人：Jack
     * 创建时间：2023/7/29 7:07 PM
     * @param array $data
     * @return array
     */
    public static function getMenuIviewList(array $data): array
    {
        return Arr::toIviewUi(Arr::getTree($data));
    }

    /**
     * 注释：转化iviewUi需要的key值
     * 创建人：Jack
     * 创建时间：2023/7/29 7:07 PM
     * @param $data
     * @return array
     */
    public static function toIviewUi($data): array
    {
        $newData = [];
        foreach ($data as $k => $v) {
            $temp = [];
            $temp['path'] = $v['menu_path'];
            $temp['name'] = $v['menu_name'];
            if ($v['pid'] == 0) {
                $temp['component'] = 'LAYOUT';
                //$temp['redirect'] = $v['menu_path'];
            } else {
                $temp['component'] = $v['menu_path'];
            }

//            $temp['component'] = $v['menu_path'];
            $temp['permission'] = $v['unique_auth'];
            $temp['meta'] = [
                'title' => $v['menu_name'],
                'icon' => $v['icon'],
                'permission' => $v['unique_auth'],
                'sort' => $v['sort'],
            ];
            $temp['orderNo'] = $v['sort'];
            $temp['hideMenu'] = (bool)$v['is_show_path'];

            if (!empty($v['children'])) {
                $temp['children'] = self::toIviewUi($v['children']);
            }
            $newData[] = $temp;
        }
        return $newData;
    }

    /**
     * 注释：获取树型菜单
     * 创建人：Jack
     * 创建时间：2023/7/29 7:08 PM
     * @param     $data
     * @param int $pid
     * @param int $level
     * @return array
     */
    public static function getTree($data, int $pid = 0, int $level = 1): array
    {
        $childs = self::getChild($data, $pid, $level);
        $dataSort = array_column($childs, 'sort');
        array_multisort($dataSort, SORT_DESC, $childs);
        foreach ($childs as $key => $navItem) {
            $resChild = self::getTree($data, $navItem['id']);
            if (null != $resChild) {
                $childs[$key]['children'] = $resChild;
            }
        }
        return $childs;
    }

    /**
     * 注释：获取子菜单
     * 创建人：Jack
     * 创建时间：2023/7/29 7:09 PM
     * @param $arr
     * @param $id
     * @param $lev
     * @return array
     */
    private static function getChild(&$arr, $id, $lev): array
    {
        $child = [];
        foreach ($arr as $k => $value) {
            if ($value['pid'] == $id) {
                $value['level'] = $lev;
                $child[] = $value;
            }
        }
        return $child;
    }

    /**
     * 格式化数据
     * @param array $array
     * @param       $value
     * @param int   $default
     * @return mixed
     */
    public static function setValeTime(array $array, $value, $default = 0)
    {
        foreach ($array as $item) {
            if (!isset($value[$item]))
                $value[$item] = $default;
            else if (is_string($value[$item]))
                $value[$item] = (float)$value[$item];
        }
        return $value;
    }

    /**
     * 获取二维数组中某个值的集合重新组成数组,并判断数组中的每一项是否为真
     * @param array  $data
     * @param string $filed
     * @return array
     */
    public static function getArrayFilterValeu(array $data, string $filed)
    {
        return array_filter(array_unique(array_column($data, $filed)), function ($item) {
            if ($item) {
                return $item;
            }
        });
    }

    /**
     * 获取二维数组中最大的值
     * @param $arr
     * @param $field
     * @return int|string
     */
    public static function getArrayMax($arr, $field)
    {
        $temp = [];
        foreach ($arr as $k => $v) {
            $temp[] = $v[$field];
        }
        if (!count($temp)) return 0;
        $maxNumber = max($temp);
        foreach ($arr as $k => $v) {
            if ($maxNumber == $v[$field]) return $k;
        }
        return 0;
    }

    /**
     * 获取二维数组中最小的值
     * @param $arr
     * @param $field
     * @return int|string
     */
    public static function getArrayMin($arr, $field)
    {
        $temp = [];
        foreach ($arr as $k => $v) {
            $temp[] = $v[$field];
        }
        if (!count($temp)) return 0;
        $minNumber = min($temp);
        foreach ($arr as $k => $v) {
            if ($minNumber == $v[$field]) return $k;
        }
        return 0;
    }

    /**
     * 注释：数组转字符串去重复
     * 创建人：Jack
     * 创建时间：2023/7/29 7:06 PM
     * @param array $data
     * @return string[]
     */
    public static function unique(array $data)
    {
        return array_unique(explode(',', implode(',', $data)));
    }

    /**
     * 获取数组中去重复过后的指定key值
     * @param array  $list
     * @param string $key
     * @return array
     */
    public static function getUniqueKey(array $list, string $key)
    {
        return array_unique(array_column($list, $key));
    }

    /**
     * 获取数组钟随机值
     * @param array $data
     * @return bool|mixed
     */
    public static function getArrayRandKey(array $data)
    {
        if (!$data) {
            return false;
        }
        mt_srand();
        $mun = rand(0, count($data));
        if (!isset($data[$mun])) {
            return self::getArrayRandKey($data);
        }
        return $data[$mun];
    }

    /**
     * 格式化数据
     * @param array $list
     * @return array
     */
    public static function formatShipping(array $list)
    {
        $freeDate = [];
        foreach ($list as $item) {
            $freeDate[$item['uniqid']][] = $item;
        }
        $data = [];
        foreach ($freeDate as $item) {
            $cityIds = [];
            $cityId = [];
            $p = [];
            foreach ($item as $value) {
                $cityId[] = $value['city_id'];
                $cityIds[] = is_array($value['value']) ? $value['value'] : json_decode($value['value'], true);
                unset($value['city_id'], $value['value']);
                $p = $value;
            }
            $p['city_id'] = $cityId;
            $p['city_ids'] = $cityIds;
            $data[] = $p;
        }
        return $data;
    }

    /**
     * 过滤字段
     * @param       $value
     * @param array $filter
     * @return mixed
     */
    public static function StrFilterValue($value, array $filter = [])
    {
        $filter = $filter ?: ['strip_tags', 'addslashes', 'trim', 'htmlspecialchars'];
        foreach ($filter as $closure) {
            if (function_exists($closure)) {
                $value = $closure($value);
            }
        }
        return $value;
    }

    /**
     * 过滤字段
     * @param array $data
     * @return array
     */
    public static function filterValue(array $data)
    {
        foreach ($data as &$item) {
            if (is_array($item)) {
                $item = self::filterValue($item);
            } else {
                $item = self::StrFilterValue($item);
            }
        }
        return $data;
    }
}